<?php


session_start();


print_r($_SESSION['install_step']);

$action = $_GET['action'];

switch ($action) {
    case 1:
		action1();
	break;
	case 2:
		action2();
	break;
	case 3:
		action3();
	break;
	case 4:
		action4();
	break;
	case 5:
		action5();
	break;
	case 6:
		action6();
	break;
}
if(isset($_GET['action'])) die('Error proccessing task');

$step = $_GET['step'];

switch ($step) {
    case 1:
		step1();
	break;
	case 2:
		step2();
	break;
	case 3:
		step3();
	break;
	case 4:
		step4();
	break;
	case 5:
		step5();
	break;
	case 6:
		step6();
	break;
	default:
		step0();
	break;
}

function step0(){


echo liteskin('Welcome to your new copy of MAGIC!',
'This installer will guide you though the basic configuration of your new magic installtion in 5 simple and easy to follow steps.<br /><br />
If you run in to trouble with the installer, a troubleshooting guide in the manual may be able to help you out. If you still have no luck,
feel free to contact us on our support forums. The more information you can provide about the error, your setup and exactly what happened
the easier it\'ll be for us to trackdown and fix the bug. <br /><br />
Best of luck, we hope you enjoy the magic product. <br /><br />
Team Magic', 
"<a href='installer.php?step=1'>Click to start installing magic!</a>");
}

function step1(){
	echo liteskin('Step 1: Set magic Paths',
	'Please end all paths and urls with a trailing /',
	"<form action='installer.php?action=1' method='post'><div>
			<fieldset>
				<legend>Basic Paths</legend>
				<label>Path to Magic</label>
				<input type='text' name='magic_path'><br/>
				<label>Path to data directory</label>
				<input type='text' name='data_path'><br/>
				<label>Website root</label>
				<input type='text' name='site_root'><br/>
				<label>Website URL</label>
				<input type='text' name='site_url' ><br/>
			</fieldset>
			<fieldset>
				<legend>Database type</legend>
				
				<label>Type of Database</label>
				<select name='db_mode'>
					<option value='mysql'>Mysql Database</option>
				</select><br/>
			</fieldset>	
			<input type='submit' name='select' value='Next'>

			</div></form>
	");
}

function file_perms($file, $octal = false)
{
    if(!file_exists($file)) return false;

    $perms = fileperms($file);

    $cut = $octal ? 2 : 3;

    return substr(decoct($perms), $cut);
}

function action1(){

$path = $_POST['magic_path'];
$mpath= $path.'magic_config.php';
if(!file_exists($mpath)) die("Path to magic incorect, could no find magic_config.php");
if(file_perms($mpath,true) !== '0777'){
	chmod($mpath, 0777) or die("unable to chmod magic_config.php in order to make it writeable. Pleace chmod the file to 777");
}
// magic path is correcrt and we have our conifg :)
if(!is_dir($_POST['data_path'])) die ('Data path is invalid');
if(!is_dir($_POST['site_root'])) die ('Site root is invalid');
//As much checking as can really do here, update are install step array
$_SESSION['install_step'] = array( 'magic_path'=>$_POST['magic_path'],
									'data_path'=>$_POST['data_path'],
									'site_root'=>$_POST['site_root'],
									'site_url'=>$_POST['site_url'],
									'db_mode'=>$_POST['db_mode'],
									);
									
header("location: installer.php?step=2");									
}

function step2(){

	if($_SESSION['install_step']['db_mode']=='mysql'){
		echo liteskin('Step 2: Configure Database Options',
		'Please provide Magic with the details needed to use your MYSQL database',
		"<form action='installer.php?action=2' method='post'><div>
				<fieldset>
					<legend>MySQL options</legend>
					<label>Mysql Username</label>
					<input type='text' name='mysql_name'><br/>
					<label>Mysql Password</label>
					<input type='text' name='mysql_pass'><br/>
					<label>Mysql Database Name</label>
					<input type='text' name='mysql_database'><br/>
					<label>MySql Host</label>
					<input type='text' name='mysql_host' value='localhost'><br/>
					<label>MySql table prefix</label>
					<input type='text' name='mysql_prefix' value='magic_'><br/>

				</fieldset>

				<input type='submit' name='select' value='Next'>

				</div></form>
		");
	}else {
		die("unsupported database");
	}
}

function action1(){

//Test connection
require('');
$path = $_SESSION['install_step']['magic_path'];//$_POST
$mpath= $path.'magic_config.php';


mysql_connect($_POST['mysql_host'], $_POST['mysql_name'], $_POST['mysql_pass']) or die("Could not connect to database");
mysql_select_db($_POST['mysql_database'], $cid) or die ("could not find database specifed, you may need to create it");


//As much checking as can really do here, update are install step array
$_SESSION['install_step']['mysql_host']= $_POST['mysql_host'];
$_SESSION['install_step']['mysql_name']= $_POST['mysql_name'];
$_SESSION['install_step']['mysql_pass']= $_POST['mysql_pass'];
$_SESSION['install_step']['mysql_database']= $_POST['mysql_database'];
$_SESSION['install_step']['mysql_prefix']= $_POST['mysql_prefix'];
									
header("location: installer.php?step=3");									
}



function step3(){
	echo liteskin('Step 3: Choose installtion mode',
	'You have now given Magic all the details it needs in order to perform a standard installtion. Please select the installtion mode
	you would like to use. Standard installtion requres no futher interaction, but does not provide advanced features such as custom
	permission modes, user attributes and other advanced cusomiseation options provided by the Advanced install option
	
	<br /><br /><br />Large images to display advanced or standard installtion<br />
	<a href="installer.php?action=3">Standard</a> - 
	<a href="installer.php?step=4">Advanced Setup</a>
	',
	"");
}

function action3(){ // simple, set everything to defualts
header("location: installer.php?step=5");
}

function step4(){
	echo liteskin('Step 4: Advanced settings',
	'',
	"
	
	<form action='installer.php?step=5' method='post'><div>
			<fieldset>
				<legend>Guest Configuation</legend>
				<label>Deafult guest name</label>
				<input type='text' name=''><br/>
				

			</fieldset>
			
			
			<fieldset>
				<legend>User Configuation</legend>
				<label>Custome user feild</label>
				<input type='text' name=''>
				<select name=''>
					<option>String</option>
					<option>Text</option>
					<option>Int</option>
				</select><br  /><br/>
				
				<label>Custom permission feild</label>
				<input type='text' name=''>
				<select name=''>
					<option>String</option>
					<option>Text</option>
					<option>Int</option>
				</select><br  /><br/>


			</fieldset>
			

			<input type='submit' name='select' value='Next'>

			</div></form>
	
	
	
	
	");
}
function action4(){ //advanced options, create additional setup stuff
header("location: installer.php?step=3");
}
function step5(){
	echo liteskin('Step 5: Configre Admin User',
	'
	',
	"<form action='installer.php?action=5' method='post'><div>
	<fieldset>
				<legend>Admin User</legend>
				<label>username</label>
				<input type='text' name='username'><br />
				<label>password</label>
				<input type='text' name='password'><br />
				
			</fieldset>	
	<input type='submit' name='select' value='Install'>

			</div></form>");
}
function action5(){ //Setup user

$_SESSION['install_step']['username']= $_POST['username'];
$_SESSION['install_step']['password']= $_POST['password'];

header("location: installer.php?step=6");
}
function step6(){

	//Write config
	//write db tables
	//write users
	//write standard perms

	echo liteskin('Step 6: Installing Database',
	'Your done! Please checkout the manuel in order to get to grips with tbe magice user system engine
	',
	"Config: written successfully<br/>
	Database: written successfully<br/>
	Admin: Created successfully<br/><br/><br/>
	Thankyou for useing MAGIC. Please delete the install file
	");
}
#Step one
//- get all system paths
//- Select database option

#step 2
//configure mysql

#step 3

//Set custom userfields
//Set guest options
//Select safemode On/Off


#step 4
//review details
//check config is writeable + data dir if requred

#step 5
//Write config
//write database - flat / mysql based on info

// Create admin in system

//Tell them to delete this file


function liteSkin($step,$text,$form){

return <<< EOF

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Install Magic - The community site engine</title>
<style type="text/css">
body,td,th {
	background-color: #d8d8d8;
	font-family: Arial, Helvetica, sans-serif;
}

#continer {
	width:80%;
	margin:auto;
	margin-top:25px;
	border:solid 1px #282627;
}
#head{
	height:120px;
	background-image:url(magic.jpg);
	background-repeat:no-repeat;
	background-color:#282627;

}
#main {
	background-color:#ffffff;
	padding:10px;
	}
	
	#main label{
	width: 180px;
	display:inline-block;

	}
#note {
		background-color:#8dc5f4;
		border:solid 1px #0ba0f5;
		padding:10px;
		font-weight:bold;
	}
#intro {
padding:10px;

}
legend {
	font-weight:bold;
}

</style>
<head>
<body>
<div id='continer'>
	<div id='head'></div>
	<div id='main'>
		<div id='note'>{$step}</div>

		<div id='intro'>
		{$text}
		</div>
		
		{$form}

	</div>

</div>

</body>

</html>

EOF;

}
?>